package org.example.system.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.example.system.entity.DealLog;
import org.example.system.entity.Payment;
import org.example.system.entity.User;
import org.example.system.service.DealLogService;
import org.example.system.service.PaymentService;
import org.example.system.service.UserService;
import org.example.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 缴费控制层
 */
@RestController
@RequestMapping("/app/payment")
@Slf4j
public class PaymentController {
    @Autowired
    private PaymentService paymentService;
    @Autowired
    private DealLogService dealLogService;
    @Autowired
    private UserService userService;

    /**
     * 缴费
     * @param payment
     * @return
     */
    @PostMapping("/pay")
    public R pay(@RequestBody Payment payment,HttpServletRequest request){
        payment.setPageNum(1L);
        payment.setPageSize(10L);
        IPage<Payment> page = paymentService.selectPage(payment);
        //根据ID查询用户
        User exitUser = userService.getOne(payment.getUserId());
        DealLog log = new DealLog();
        //存在缴费，直接缴费，不存在则先创建
        if (page.getRecords().size()==1){
            Payment exitPayment = page.getRecords().get(0);
            if (payment.getWaterFee()!=null){
                exitPayment.setWaterFee(exitPayment.getWaterFee() + payment.getWaterFee());
            }
            if(payment.getElectricFee()!=null) {
                exitPayment.setElectricFee(exitPayment.getElectricFee() + payment.getElectricFee());
            }
            payment.setUpdateBy(exitUser.getUsername());
            paymentService.update(exitPayment);
        }else{
            if (payment.getWaterFee()!=null){
                payment.setWaterFee(payment.getWaterFee());
            }
            if(payment.getElectricFee()!=null) {
                payment.setElectricFee(payment.getElectricFee());
            }
            payment.setCreateBy(exitUser.getUsername());
            paymentService.add(payment);
        }
        if (payment.getWaterFee()!=null){
            exitUser.setMoney(exitUser.getMoney()-payment.getWaterFee());
            log.setBlock("缴水费");
            log.setMoney(payment.getWaterFee());
        }
        if(payment.getElectricFee()!=null) {
            exitUser.setMoney(exitUser.getMoney()-payment.getElectricFee());
            log.setBlock("缴电费");
            log.setMoney(payment.getElectricFee());
        }
        userService.update(exitUser);
        log.setUsername(exitUser.getUsername());
        log.setInfo("余额支付");
        dealLogService.add(log,request);
        return R.ok(page);
    }

    /**
     * 查询
     * @param payment
     * @return
     */
    @GetMapping("/select")
    public R list(Payment payment){
        payment.setPageNum(1L);
        payment.setPageSize(10L);
        IPage<Payment> page = paymentService.selectPage(payment);
        log.info("------------ PaymentController | list | selectPage | return : {} ", JSON.toJSONString(page));
        return R.ok(page);
    }
}
